package cn.manyufun.myf.ui.main.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.manyufun.myf.R;
import cn.manyufun.myf.bean.ComicsSummary;
import cn.manyufun.myf.bean.SearchBean;
import cn.manyufun.myf.common.base.BaseActivity;
import cn.manyufun.myf.common.commonwidget.LoadingTip;
import cn.manyufun.myf.common.irecyclerview.IRecyclerView;
import cn.manyufun.myf.common.irecyclerview.OnLoadMoreListener;
import cn.manyufun.myf.common.irecyclerview.OnRefreshListener;
import cn.manyufun.myf.common.irecyclerview.widget.LoadMoreFooterView;
import cn.manyufun.myf.ui.main.adapter.FollowListAdapter;
import cn.manyufun.myf.ui.main.contract.SearchContract;
import cn.manyufun.myf.ui.main.model.SearchModel;
import cn.manyufun.myf.ui.main.presenter.SearchPresenter;

public class SearchResultActivity extends BaseActivity<SearchPresenter, SearchModel> implements SearchContract.View, OnRefreshListener, OnLoadMoreListener {
    private static final int PAGE_SIZE = 20;
    @BindView(R.id.irc)
    IRecyclerView irc;
    @BindView(R.id.loadedTip)
    LoadingTip loadedTip;
    @BindView(R.id.et_search)
    EditText mEtSearch;
    @BindView(R.id.btn_cancel)
    TextView mBtnCancel;

    @OnClick(R.id.btn_cancel)
    void cancel() {
        finish();
    }


    private FollowListAdapter searchListAdapter;
    private int mStartPage = 1;
    private List<ComicsSummary> datas = new ArrayList<>();
    private String mQueryStr;

    /**
     * 入口
     *
     * @param context
     */
    public static void startAction(Context context) {
        Intent intent = new Intent(context, SearchResultActivity.class);
        context.startActivity(intent);
    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_search_result;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    public void initView() {
        initToolbar();
        irc.setLayoutManager(new LinearLayoutManager(this));
        datas.clear();
        searchListAdapter = new FollowListAdapter(this, R.layout.common_comics_item, datas);
//        newListAdapter.openLoadAnimation(new ScaleInAnimation());

        irc.setAdapter(searchListAdapter);
        irc.setOnRefreshListener(this);
        irc.setOnLoadMoreListener(this);

        mEtSearch.setOnKeyListener(new View.OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {

                    // 1. 点击搜索按键后，根据输入的搜索字段进行查询
                    // 注：由于此处需求会根据自身情况不同而不同，所以具体逻辑由开发者自己实现，此处仅留出接口
                    if (!TextUtils.isEmpty(mEtSearch.getText().toString())) {
                        mQueryStr = mEtSearch.getText().toString();
                        mStartPage = 1;
                        mPresenter.loadSearchRequest(mStartPage, PAGE_SIZE, mQueryStr);
                    }

                }
                return false;
            }
        });

    }

    private void initToolbar() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            toolbar.setTitle("");
            setSupportActionBar(toolbar);
        }
    }

    @Override
    public void onRefresh() {
        if (TextUtils.isEmpty(mQueryStr))
            return;
        searchListAdapter.getPageBean().setRefresh(true);
        mStartPage = 1;
        //发起请求
        irc.setRefreshing(true);
        mPresenter.loadSearchRequest(mStartPage, PAGE_SIZE, mQueryStr);
    }

    @Override
    public void onLoadMore(View loadMoreView) {
        if (TextUtils.isEmpty(mQueryStr))
            return;
        searchListAdapter.getPageBean().setRefresh(false);
        //发起请求
        irc.setLoadMoreStatus(LoadMoreFooterView.Status.LOADING);
        mPresenter.loadSearchRequest(mStartPage, PAGE_SIZE, mQueryStr);
    }

    @Override
    public void showLoading(String title) {
        if (searchListAdapter.getPageBean().isRefresh()) {
            if (searchListAdapter.getSize() <= 0) {
                loadedTip.setLoadingTip(LoadingTip.LoadStatus.loading);
            }
        }
    }

    @Override
    public void stopLoading() {
        if (mStartPage == 1 && searchListAdapter.getSize() <= 0)
            loadedTip.setLoadingTip(LoadingTip.LoadStatus.empty);
        else
            loadedTip.setLoadingTip(LoadingTip.LoadStatus.finish);

        mEtSearch.requestFocus();
    }

    @Override
    public void showErrorTip(String msg) {
        if (searchListAdapter.getPageBean().isRefresh()) {
            if (searchListAdapter.getSize() <= 0) {
                loadedTip.setLoadingTip(LoadingTip.LoadStatus.error);
                loadedTip.setTips(msg);
            }
            irc.setRefreshing(false);
        } else {
            irc.setLoadMoreStatus(LoadMoreFooterView.Status.ERROR);
        }
    }

    @Override
    public void returnSearchResult(SearchBean searchBean) {
        List<ComicsSummary> newsSummaries = searchBean.getComicList();
        if (newsSummaries != null && newsSummaries.size() > 0) {
            mStartPage += 20;
            if (searchListAdapter.getPageBean().isRefresh()) {
                irc.setRefreshing(false);
                searchListAdapter.replaceAll(newsSummaries);
                irc.setLoadMoreStatus(LoadMoreFooterView.Status.GONE);
            } else {
                if (newsSummaries.size() > 0) {
                    irc.setLoadMoreStatus(LoadMoreFooterView.Status.GONE);
                    searchListAdapter.addAll(newsSummaries);
                } else {
                    irc.setLoadMoreStatus(LoadMoreFooterView.Status.THE_END);
                }
            }
        } else {
            irc.setRefreshing(false);
            if (mStartPage == 1) {
                searchListAdapter.clear();
                loadedTip.setLoadingTip(LoadingTip.LoadStatus.empty);
                irc.setLoadMoreStatus(LoadMoreFooterView.Status.GONE);
            } else {
                irc.setLoadMoreStatus(LoadMoreFooterView.Status.THE_END);
            }
        }
    }
}
